package com.saer.client.button;

import com.google.gwt.core.client.GWT;
import com.saer.client.RemoteWebService;
import com.saer.client.RemoteWebServiceAsync;
import com.saer.client.dto.SearchDto;
import com.saer.client.tab.BaseTab;
import com.saer.client.tab.SearchInput;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

public abstract class BaseSearchButton extends Button {

	protected final RemoteWebServiceAsync remoteWebService = GWT.create(RemoteWebService.class);
	protected BaseTab sourceTab;
	protected SearchInput searchInput;
	protected SearchDto searchDto;

	public BaseSearchButton(BaseTab vSourceTab, SearchInput vSearchInput) {
		super("Submit");
		this.setIcon("icons/16/search.png");
		this.setShowDisabledIcon(false);
		
		this.searchInput = vSearchInput;
		this.sourceTab = vSourceTab;

		this.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				sourceTab.setTabTitleSuffix("");

				if (searchInput.validate()) {
					sourceTab.getTabCanvas().setShowHover(false);
					sourceTab.showWorkingWidget();

					searchDto = searchInput.getData();

					callBackend(searchDto);
				}
			}
		});
	}

	//Override it
	protected abstract void callBackend (SearchDto searchDto);

}
